class Solution {
public:
    vector<int> preorder(Node* root) {
        int i;
vector<int> r;    
        stack<Node*> q;
        if (root) q.push(root);
        while (!q.empty()) {
            Node* cur = q.top();  
            q.pop();
            r.push_back(cur->val);
            for ( i = cur->children.size() - 1; i >= 0; i--) { 
                if (cur->children[i]) q.push(cur->children[i]); 
            }
        }
        return r;        
    }
};
